package gui;

import gestionBDD.*;
import interne.*;

import java.sql.*;
import java.util.*;

import com.trolltech.qt.gui.*;

public class fenetreAlbumPriv extends QWidget {
	
	Client client ;
	Album album ;
	QListWidget albumListWidget;
	QTextBrowser detailsTextDocument ;

	
	public fenetreAlbumPriv(Client c) throws SQLException {

		setClient(c) ;
		
		albumListWidget = new QListWidget();
        albumListWidget.currentItemChanged.connect(this, "afficherDetails()");

		detailsTextDocument = new QTextBrowser();
		
        /** Login's GroupBox' **/
        QGroupBox albumsGroup = new QGroupBox(tr("Albums personnels"));
        QHBoxLayout albumsLayout = new QHBoxLayout();
 
        albumsLayout.addWidget(albumListWidget);
        albumsLayout.addWidget(detailsTextDocument);
        albumsGroup.setLayout(albumsLayout);
        
        QGroupBox actionsGroup = new QGroupBox(tr("Actions"));
   
        /* Bouton Connexion */
        QPushButton boutonGererContenu = new QPushButton("Gérer Contenu");
        boutonGererContenu.clicked.connect(this, "gererContenu()");  
        
        /* Bouton Editer Album */
        QPushButton boutonEditer = new QPushButton("Editer l'album");
        boutonEditer.clicked.connect(this, "editerAlbum()");  
        
        /* Bouton Créer Album */
        QPushButton boutonAjouterAlbum = new QPushButton("Créer un album...");
        boutonAjouterAlbum.clicked.connect(this, "popAjouterAlbum()");
        
        /* Bouton Supprimer Album */
        QPushButton boutonRetirerAlbum = new QPushButton("Supprimer");
        boutonRetirerAlbum.clicked.connect(this, "supprimerAlbum()");
        
        /* Bouton Rafraichir */
        QPushButton boutonRefresh = new QPushButton("Refresh");
        boutonRefresh.clicked.connect(this, "reactualiser()"); 
        /* Bouton Retour */
        QPushButton boutonRetour = new QPushButton("Retour");
        boutonRetour.clicked.connect(this, "retour()");  

        QVBoxLayout actionsLayout = new QVBoxLayout();
        actionsLayout.addWidget(boutonGererContenu);
        actionsLayout.addWidget(boutonEditer);
        actionsLayout.addWidget(boutonAjouterAlbum);
        actionsLayout.addWidget(boutonRetirerAlbum);
        actionsLayout.addWidget(boutonRefresh);
        actionsLayout.addWidget(boutonRetour);
        actionsGroup.setLayout(actionsLayout);
        
        QVBoxLayout layout = new QVBoxLayout();

        layout.addWidget(albumsGroup);
        layout.addWidget(actionsGroup);
        setLayout(layout);
        
		List<Album> listeAlbum = new ArrayList<Album>(); 
		listeAlbum = extractionBDD.extraireAlbumsPriv(client);
		Iterator<Album> ite = listeAlbum.iterator();
		while (ite.hasNext()) {
		    Album album = ite.next();
		    albumListWidget.addItem(album.getTitreAlbum() + " / " + album.getIdAlbum()) ;
		}
	}
	
	public void afficherDetails() throws SQLException {
		String temp[] = albumListWidget.currentItem().text().split(" / ") ;
		Integer idAlbum = Integer.parseInt(temp[1]) ;
		Album album = new Album(idAlbum) ;
		album = requetesAlbum.trouverAvecIdAlbum(album) ;
		setAlbum(album) ;
		String details = "Titre : \t" + album.getTitreAlbum() + "\n" +
				"Sous-tire : \t" + album.getSousTitre() + "\n" +
				"Partage : \t" + album.getPartage().toString() + "\n" +
				"Auteur : \t" + album.getIdClient();
		if (album.getCouverture() != null && album.getCouverture() != 0) {
			details = details +
					"\nCouverture : " + requetesContient.retourneTitreImage(album.getCouverture()) + "\n" +
					"Préface : " + album.getPreface() + "\n" +
					"Postface : " + album.getPostface() ;
		}
		detailsTextDocument.setText(details) ;
	}
	
	public void retour() throws SQLException {
        fenetreClient fenetre = new fenetreClient(client);
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}
	
	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public void gererContenu() throws SQLException {
		try {
			fenetrePhoto fenetre = new fenetrePhoto(client, album);
			QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
			menuPrincipal.setCentralWidget(fenetre) ;
		}
		catch(SQLException e) {
			System.out.println(e) ;
		}
	}
	
    public void editerAlbum() throws SQLException {
		String temp[] = albumListWidget.currentItem().text().split(" / ") ;
		Integer idAlbum = Integer.parseInt(temp[1]) ;
		Album album = new Album(idAlbum) ;
		album = requetesAlbum.trouverAvecIdAlbum(album) ;
		setAlbum(album) ;
    	fenetreAlbumEdition fenetre = new fenetreAlbumEdition(album) ;
    	fenetre.show() ;
    }


    public void popAjouterAlbum() throws SQLException {
		fenetreAjoutAlbum fenetre = new fenetreAjoutAlbum(client) ;
		fenetre.show() ;   		
    }
    
    public void supprimerAlbum() throws SQLException {
		String temp[] = albumListWidget.currentItem().text().split(" / ") ;
		Integer idAlbum = Integer.parseInt(temp[1]) ;  	
		Album a = new Album(idAlbum) ;
		requetesContient.supprimerContientAlbum(a) ;
		requetesAlbum.supprimerAlbum(a) ;
		reactualiser() ;
    }
    
	public void reactualiser() throws SQLException {
        fenetreAlbumPriv fenetre = new fenetreAlbumPriv(client);
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}
}
